package com.work;

import com.constant.Constants;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

//work queue:一个生产者,多个消费者
public class product {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(Constants.HOST);
        connectionFactory.setPort(Constants.POST);
        connectionFactory.setUsername(Constants.USER_NAME);
        connectionFactory.setPassword(Constants.PASSWORD);
        connectionFactory.setVirtualHost(Constants.VIRTUAL_HOST);
        Connection connection = connectionFactory.newConnection();
        //2.开启通道
        Channel channel = connection.createChannel();
        //3.声明交换机
        //4.声明队列
        channel.queueDeclare(Constants.WORK_QUEUE,true,false,false,null);
        //5.发送消息
        //发送10条消息到rabbitmq
        for(int i=0;i<10;i++){
            String msg = "hello work "+i ;
            /**
             * basicPublish(String var1, String var2, AMQP.BasicProperties var3, byte[] var4)
             * 参数说明         交换机名称       内置交换机       属性配置        消息
             */
            channel.basicPublish("",Constants.WORK_QUEUE,null,msg.getBytes());
        }
        System.out.println("发送消息成功!");
        //6.释放资源
        //(这两个资源关闭的顺序不能反,channel必须在connection前面,或者只关闭connection.
        channel.close();
        connection.close();;
    }
}
